// 统一导航栏管理系统
class NavigationManager {
    constructor() {
        this.currentPage = this.getCurrentPage();
        this.init();
    }

    // 初始化导航栏
    init() {
        this.setupActiveLinks();
        this.setupNavigationEvents();
        this.setupResponsiveMenu();
    }

    // 获取当前页面名称
    getCurrentPage() {
        const path = window.location.pathname;
        const page = path.split('/').pop() || 'dashboard.html';
        return page;
    }

    // 设置活动链接状态
    setupActiveLinks() {
        const navLinks = document.querySelectorAll('.sidebar a');
        navLinks.forEach(link => {
            // 移除所有active类
            link.classList.remove('active');
            
            // 检查链接是否指向当前页面
            const href = link.getAttribute('href');
            if (href === this.currentPage || 
                (this.currentPage === 'index.html' && href === 'dashboard.html') ||
                (this.currentPage === 'dashboard.html' && href === 'index.html')) {
                link.classList.add('active');
            }
        });
    }

    // 设置导航事件
    setupNavigationEvents() {
        // 为所有导航链接添加点击事件
        document.addEventListener('click', (e) => {
            if (e.target.matches('.sidebar a') || e.target.closest('.sidebar a')) {
                const link = e.target.matches('.sidebar a') ? e.target : e.target.closest('.sidebar a');
                const href = link.getAttribute('href');
                
                // 如果是内部链接，处理导航
                if (href && !href.startsWith('#') && !href.startsWith('http')) {
                    e.preventDefault();
                    this.navigateTo(href);
                }
            }
        });

        // 键盘导航支持
        document.addEventListener('keydown', (e) => {
            if (e.altKey) {
                switch(e.key) {
                    case '1': this.navigateTo('dashboard.html'); break;
                    case '2': this.navigateTo('reports.html'); break;
                    case '3': this.navigateTo('blog.html'); break;
                    case '4': this.navigateTo('products.html'); break;
                    case '5': this.navigateTo('orders.html'); break;
                    case '6': this.navigateTo('logs.html'); break;

                    case '8': this.navigateTo('system-settings.html'); break;
                }
            }
        });
    }

    // 导航到指定页面
    navigateTo(page) {
        // 显示加载状态
        this.showLoading();
        
        // 延迟导航以显示加载效果
        setTimeout(() => {
            window.location.href = page;
        }, 300);
    }

    // 显示加载状态
    showLoading() {
        // 创建加载遮罩
        const loadingOverlay = document.createElement('div');
        loadingOverlay.className = 'navigation-loading';
        loadingOverlay.innerHTML = `
            <div class="loading-spinner">
                <i class="fas fa-spinner fa-spin"></i>
                <span>页面加载中...</span>
            </div>
        `;
        
        // 添加样式
        loadingOverlay.style.cssText = `
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0,0,0,0.7);
            display: flex;
            align-items: center;
            justify-content: center;
            z-index: 9999;
            color: white;
            font-size: 18px;
        `;
        
        document.body.appendChild(loadingOverlay);
        
        // 3秒后自动移除（防止卡死）
        setTimeout(() => {
            if (loadingOverlay.parentNode) {
                loadingOverlay.remove();
            }
        }, 3000);
    }

    // 设置响应式菜单
    setupResponsiveMenu() {
        // 创建移动端菜单按钮
        const menuToggle = document.createElement('button');
        menuToggle.className = 'mobile-menu-toggle';
        menuToggle.innerHTML = '<i class="fas fa-bars"></i>';
        menuToggle.style.cssText = `
            display: none;
            position: fixed;
            top: 10px;
            left: 10px;
            z-index: 1000;
            background: #2c3e50;
            color: white;
            border: none;
            padding: 10px;
            border-radius: 4px;
            cursor: pointer;
        `;
        
        document.body.appendChild(menuToggle);
        
        // 响应式处理
        this.handleResponsive();
        window.addEventListener('resize', () => this.handleResponsive());
        
        // 菜单切换事件
        menuToggle.addEventListener('click', () => {
            const sidebar = document.querySelector('.sidebar');
            sidebar.classList.toggle('mobile-open');
        });
    }

    // 处理响应式布局
    handleResponsive() {
        const sidebar = document.querySelector('.sidebar');
        const menuToggle = document.querySelector('.mobile-menu-toggle');
        
        if (window.innerWidth <= 768) {
            menuToggle.style.display = 'block';
            sidebar.style.transform = 'translateX(-100%)';
            sidebar.style.transition = 'transform 0.3s ease';
        } else {
            menuToggle.style.display = 'none';
            sidebar.style.transform = 'translateX(0)';
            sidebar.classList.remove('mobile-open');
        }
    }

    // 获取导航配置
    getNavigationConfig() {
        return [
            { name: '仪表盘', href: 'dashboard.html', icon: 'fas fa-tachometer-alt', shortcut: 'Alt+1' },
            { name: '访客统计', href: 'reports.html', icon: 'fas fa-chart-line', shortcut: 'Alt+2' },
            { name: '博客管理', href: 'blog.html', icon: 'fas fa-blog', shortcut: 'Alt+3' },
            { name: '产品管理', href: 'products.html', icon: 'fas fa-box', shortcut: 'Alt+4' },
            { name: '订单管理', href: 'orders.html', icon: 'fas fa-shopping-cart', shortcut: 'Alt+5' },
            { name: '网站日志', href: 'logs.html', icon: 'fas fa-file-alt', shortcut: 'Alt+6' },
            { name: '系统设置', href: 'system-settings.html', icon: 'fas fa-cog', shortcut: 'Alt+9' }
        ];
    }
}

// 页面加载完成后初始化导航
if (document.querySelector('.sidebar')) {
    window.navigationManager = new NavigationManager();
}

// 导出供其他脚本使用
if (typeof module !== 'undefined' && module.exports) {
    module.exports = NavigationManager;
}